import request, { all } from '@/utils/request'

/**
 * 商品列表初始数据
 * @param params
 * @param callback
 */
export function initial(callback, params) {
  const url = [
    request.get('/goods/paginate', { params }),
    request.get('/supplier/paginate', { params: { status: 1, is_list: 1 }})
  ]
  all(url, function(res) {
    if (res !== false) {
      callback(res)
    }
  })
}

/**
 * 创建商品
 * @param data
 * @returns AxiosPromise
 */
export function store(data) {
  return request({
    url: '/goods/store',
    method: 'post',
    data: data
  })
}

/**
 * 修改商品
 * @param id
 * @returns AxiosPromise
 */
export function updateGoods(params) {
  return request({
    url: '/goods/update/' + params.goods_id,
    method: 'post',
    data: params
  })
}

/**
 * 商品详情
 * @param params
 * @returns AxiosPromise
 */
export function info(params) {
  return request.get('/goods/info', { params })
}

/**
 * 商品分页
 * @param params
 * @returns AxiosPromise
 */
export function paginate(params) {
  return request.get('/goods/paginate', { params })
}

/**
 * 商品统计
 * @param params
 * @returns AxiosPromise
 */
export function getGoodsSummary(params) {
  return request.get('/statistics/goods/summary', { params })
}

/**
 * 数据导出
 * @param params
 * @returns AxiosPromise
 */
export function exportList(params) {
  return request({
    url: '/goods/export',
    method: 'post',
    params: params
  })
}

/**
 * 打印标签
 * @param params
 * @param mode
 * @param callback
 * @returns AxiosPromise
 */
export function printTemplate(params, mode, callback) {
  const url = [
    request.get('/goods/paginate', { params }),
    request.get('/print/tpl/paginate?is_list=1&mode=' + mode)
  ]
  all(url, function(res) {
    if (res !== false) {
      callback(res)
    }
  })
}

/**
 * 删除数据
 * @param data
 * @returns AxiosPromise
 */
export function destroy(id) {
  return request({
    url: '/goods/destroy/' + id,
    method: 'delete'
  })
}

/**
 * 批量更新
 * @param data
 * @returns AxiosPromise
 */
export function updateAll(data) {
  return request({
    url: '/goods/update/all',
    method: 'post',
    data: data
  })
}

/**
 * 批量删除
 * @param data
 * @returns AxiosPromise
 */
export function destroyAll(data) {
  return request({
    url: '/goods/destroy/all',
    method: 'delete',
    data: data
  })
}
